$(function() {

	var index = $("#subaccountStatus").val();
	$("#subaccount_status_div a:eq(" + index + ")").addClass("aon");

	$(".menu_list").find(".lion").removeClass("lion");
	//$(".menu_list").find("li:eq(3)").addClass("lion");
	$("#subaccounts").addClass("lion");
	// 按定单状态搜索定单
	$("#subaccount_status_div a").click(function() {
				var i = $(this).index();
				var status = i - 1;
				$("#subaccount_status_div a").removeClass("aon");
				$(this).addClass("aon");
				$("#subaccountStatus").val(status);
				$("#query_subaccounts_form").submit();
				return false;
			});

	$("#btn_search").click(function() {
				$("#query_subaccounts_form").submit();
			});
	});
	
//单个删除子帐号
function deleteSubAccount(subAccountsId){
	var dialog = art.dialog({
			title : '提示',
			content : "您确定要删除该子帐号吗？",
			lock : true,
			drag : false,
			icon : 'question',
			ok : function() {
				$.ajax({
				   type: "POST",
				   url: "usercenter/subaccounts/delete",
				   data: {
				   		pkId:subAccountsId
				   },
				   success: function(data){
				   	   if(data.result === true){
				   	       $("#totalItemsSize").val($("#totalItemsSize").val()-1); 
					   	   $.post("usercenter/subaccounts/page?t=" + Math.random(), {
									page :Number($("#page").val()),
									totalResults : Number(data.result)
									}, function(data) {
									   alertMsg("删除子账号成功!");
										$("#div_page_container").empty().html(data);
										initPagination();
									});
									if($("#totalItemsSize").val()==0){
									$("#Pagination").hide();
									}
				   	   }else{
				   	   		alertMsg(data.result);
				   	   }
			   	   }
				});
				
			},
			cancel : true
	});
}

//批量删除子帐号
function batchDeleteSubAccounts(){
	
	var subAccountIds = new Array();
	$("input[name='checks']").each(function(){
		if($(this).attr("checked")){
			subAccountIds.push($(this).attr("subAccountId"));
		}
	});
	
	if(subAccountIds.length == 0 ){
		alertMsg("请选择一条记录！");
		return;
	}
	
	var dialog = art.dialog({
			title : '提示',
			content : "您确定要删除选中的子帐号吗？",
			lock : true,
			drag : false,
			icon : 'question',
			ok : function() {
				$.ajax({
				   type: "POST",
				   url: "usercenter/subaccounts/batchDelete",
				   data: {
				   		subAccountIds:subAccountIds.join(",")
				   },
				   success: function(data){
				   	   if(data.result === true){
				   	      $.post("usercenter/subaccounts/count?t=" + Math.random(),{},
				   	      function(data){
				   	      		$("#totalItemsSize").val(data.result);
				   	      		$.post("usercenter/subaccounts/page?t=" + Math.random(), {
										page :Number($("#page").val()),
										totalResults : Number(data.result)
									}, function(data) {
									   alertMsg("删除联系人成功!");
										$("#div_page_container").empty().html(data);
										initPagination();
										 
									});
									if(data.result==0){
									$("#Pagination").hide();
									}
				   	      });

				   	   }else{
				   	   		alertMsg(data.result);
				   	   }
			   	   }
				});
			},
			cancel : true
		});
}

//复选框选择
function checkAllOrNot(){
	var checkState = $("#checkAll")[0].checked;
	$("input[name='checks']").each(function(){
		$(this).attr("checked",checkState);
	});
}


// 初始化分页组件
function initPagination() {


	var num_entries = Number($("#totalItemsSize").val());
	var eachPageSize = Number($("#eachPageSize").val());
	var prePage = $("#page").val();
	var current_page_val = 0;
	if (prePage > 1) {
		current_page_val = Number(prePage) - 1;
	}
	if (num_entries > 0) {
		var totalPageVal = parseInt(num_entries / eachPageSize);
		if ((num_entries % eachPageSize) > 0) {
			totalPageVal = totalPageVal + 1;
		}
		if (current_page_val >= totalPageVal) {
			current_page_val = totalPageVal - 1;
		}
		$("#Pagination").pagination(num_entries, {
					num_edge_entries : 1,
					num_display_entries : 3,
					callback : pageselectCallback,
					items_per_page : eachPageSize,
					current_page : current_page_val,
					prev_text : "上一页",
					next_text : "下一页"
				});
	} else {
	    $("#Pagination").hide();
	}
}

// 分页回调函数
function pageselectCallback(page_index, jq) {
	loadEachPageAccountItemInfo(page_index + 1);
	return false;
}




// 异步加载分页数据
function loadEachPageItemInfo(pageNum) {
	$.post("usercenter/subaccounts/page?t=" + Math.random(), {
				page : pageNum,
				totalResults : Number($("#totalItemsSize").val())
			}, function(data) {
				$("#div_page_container").empty().html(data);
				$("#page").val(pageNum);
			});
}
